package controller

import (
	"user-go/entity"

	"github.com/androidsr/sc-go/model"
	"github.com/androidsr/sc-go/sbuilder"
	"github.com/androidsr/sc-go/sgin"
	"github.com/androidsr/sc-go/sno"
	"github.com/androidsr/sc-go/sorm"
	"github.com/gin-gonic/gin"
)

func init() {
	sgin.AddRouter(SysParamsController{})
}

type SysParamsController struct {
}

// 查询
// @Router [get] [/sysparams/:id]
func (SysParamsController) Get(c *gin.Context) any {
	id := c.Param("id")
	data := new(entity.SysParams)
	data.Id = id
	err := sorm.DB.GetOne(data)
	if err != nil {
		return model.NewFailDefault("查询失败")
	}
	return data
}

// 增加
// @Router [post] [/sysparams]
func (SysParamsController) Post(c *gin.Context, in *entity.SysParams) any {
	in.Id = sno.GetString()
	sorm.DB.Insert(in)
	return model.NewOK(nil)
}

// 修改
// @Router [put] [/sysparams/edit]
func (SysParamsController) Put(c *gin.Context, in *entity.SysParams) any {
	sorm.DB.UpdateById(in)
	return model.NewOK(nil)
}

// 删除
// @Router [delete] [/sysparams/:id]
func (SysParamsController) Delete(c *gin.Context) any {
	id := c.Param("id")
	if id == "" {
		return model.NewFailDefault("主键不能为空")
	}
	sorm.DB.Delete(entity.SysParams{Id: id})
	return model.NewOK(nil)
}

type SysParamsQuery struct {
	Page      model.PageInfo   `json:"page"`
	QueryData entity.SysParams `json:"queryData"`
}

// 查询列表
// @Router [post] [/sysparams/page]
func (SysParamsController) QueryTables(c *gin.Context, in *SysParamsQuery) any {
	sql := `select * from sys_params a where 1=1 `
	data := make([]entity.SysParams, 0)
	b := sbuilder.StructToBuilder(in.QueryData, sql)
	sql, values := b.Build()
	return sorm.DB.SelectPage(&data, in.Page, sql, values...)
}

// 分页下拉
// @Router [post] [/sysparams/list]
func (SysParamsController) QuerySelect(c *gin.Context, in *model.SelectQueryDTO) any {
	sql := "select id as value,name as label from sys_params where 1=1 and id is not null"
	data := make([]model.SelectVO, 0)
	b := sbuilder.Builder(sql)
	b.Like("CONCAT(id,name)", in.Label)
	b.Multiple().Ors(b.In("id", in.Selected))
	sql, values := b.Build()
	return sorm.DB.SelectPage(&data, in.Page, sql, values...)
}

// @Router [post] [/sysparams/select/:id]
func (SysParamsController) SelectByGroup(c *gin.Context) any {
	id := c.Param("id")
	data := make([]entity.SysParams, 0)
	err := sorm.DB.SelectList(&data, entity.SysParams{GroupId: id})
	if err != nil {
		return model.NewFailDefault("查询数据失败")
	}
	return model.NewOK(nil)
}

// 加载全部参数
// @Router [post] [/sysparams/getall]
func (SysParamsController) GetAll(c *gin.Context) any {
	data := make([]entity.SysParams, 0)
	err := sorm.DB.SelectList(&data, entity.SysParams{})
	if err != nil {
		return model.NewFailDefault("查询数据失败")
	}
	result := make(map[string][]model.SelectVO, 0)
	for _, v := range data {
		item := make([]model.SelectVO, 0)
		for _, vv := range data {
			if v.GroupId == vv.GroupId {
				item = append(item, model.SelectVO{Label: vv.Title, Value: vv.KeyId})
			}
		}
		result[v.GroupId] = item
	}
	return result
}
